﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql.Internal.Model;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Core.Extension;
using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OfficeOpenXml;

namespace Bzzb.Controllers
{
    public class DingQiWeiHuJiLuController : BaseController
    {
        private readonly IWebHostEnvironment _env;
        private readonly IConfiguration _configuration;
        private readonly ILogger<DingQiWeiHuJiLuController> _logger;
        public DingQiWeiHuJiLuController(ILogger<DingQiWeiHuJiLuController> logger, IFreeSql fsql, IConfiguration configuration, IWebHostEnvironment env) : base(fsql)
        {
            this._env = env;
            this._logger = logger;
            this._configuration = configuration;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index([FromForm] long bzzbtid, [FromForm] long bzzbid, [FromForm] long jtid, [FromForm] string content, [FromForm] int pageIndex = 1, [FromForm] int pageSize = 10)
        {
            DynamicFilterInfo dyfilter = JsonConvert.DeserializeObject<DynamicFilterInfo>(@"
{
  'Logic' : 'And',
  'Filters' :
  [
    {
      'Field' : 'kzxmc',
      'Operator' : 'Contains',
      'Value' : '" + content + @"',
    }
  ]
}
");
            List<DingQiWeiHuJiLu> dingqiweihujilus = await _fsql.Select<DingQiWeiHuJiLu>().Where(dqwhjl => dqwhjl.Bzzbtid == bzzbtid && dqwhjl.Bzzbid == bzzbid && dqwhjl.Jtid == jtid && dqwhjl.Gjid == 0).WhereDynamicFilter(dyfilter).OrderBy("id desc").Count(out var total).Page(pageIndex, pageSize).ToListAsync();
            _logger.LogInformation(_fsql.Select<DingQiWeiHuJiLu>().WhereDynamicFilter(dyfilter).Page(pageIndex, pageSize).ToSql());
            JObject data = new JObject();
            data.Add("total", total);
            data.Add("list", JArray.FromObject(dingqiweihujilus));
            return RenderJson(data: data);
        }

        /// <summary>
        /// 获取所有未使用的工具
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
//        [HttpPost]
//        public async Task<IDictionary<string, Object>> GetAllUnusedDingQiWeiHuJiLus([FromForm] string content)
//        {
//            DynamicFilterInfo dyfilter = JsonConvert.DeserializeObject<DynamicFilterInfo>(@"
//{
//  'Logic' : 'And',
//  'Filters' :
//  [
//    {
//      'Field' : 'name',
//      'Operator' : 'Contains',
//      'Value' : '" + content + @"',
//    }
//  ]
//}
//");
//            List<DingQiWeiHuJiLu> dingqiweihujilus = await _fsql.Select<DingQiWeiHuJiLu>().Where(dqwhjl => dqwhjl.IsUsed == 0).WhereDynamicFilter(dyfilter).ToListAsync();
//            _logger.LogInformation(_fsql.Select<DingQiWeiHuJiLu>().Where(dqwhjl => dqwhjl.IsUsed == 0).WhereDynamicFilter(dyfilter).ToSql());
//            return RenderJson(data: dingqiweihujilus);
//        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Save([FromForm] DingQiWeiHuJiLu dingqiweihujilu)
        {
            long id = await _fsql.Insert(dingqiweihujilu).ExecuteIdentityAsync();
            dingqiweihujilu.Id = id;

            return RenderJson(data: dingqiweihujilu);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Delete([FromForm] long id) {
            int affrows = await _fsql.Delete<DingQiWeiHuJiLu>().Where(dqwhjl => dqwhjl.Id == id).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Update([FromForm] DingQiWeiHuJiLu dingqiweihujilu) {
            int affrows = await _fsql.Update<DingQiWeiHuJiLu>().SetSourceIgnore(dingqiweihujilu, colval => colval == null).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Show([FromForm] long id) {
            DingQiWeiHuJiLu dingqiweihujilu = await _fsql.Select<DingQiWeiHuJiLu>().Where(dqwhjl => dqwhjl.Id == id).ToOneAsync();
            return RenderJson(data: dingqiweihujilu);
        }
    }
}
